package com.fruit.controller.wap.logistics; 
import com.fruit.pojo.BaseDTO;
import com.fruit.pojo.logistics.LogisticsType;
import com.fruit.pojo.logistics.SysRoute;
import com.fruit.pojo.logistics.SysRouteResponse;
import com.fruit.service.logistics.LogisticsService;
import com.joe.utils.common.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 物流controller
 *
 * @author joe
 * @version 2018.02.28 14:13
 */
@Slf4j
@Controller
@RequestMapping(value = "/wap/logistics/")
public class LogisticsController {
    @Autowired
    private LogisticsService logisticsService;

    /**
     * 获取订单快递信息
     *
     * @param type    订单使用的快递商家
     * @param orderId 订单ID（不能为空）
     * @return 订单的快递信息
     */
    @RequestMapping(value = "getRouter", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public BaseDTO<List<SysRoute>> getRouter(@RequestParam("type") LogisticsType type, @RequestParam("orderId")
            String orderId) {
        if (type == null || StringUtils.isEmpty(orderId)) {
            log.warn("用户获取路由信息参数为空");
            return BaseDTO.buildError("参数错误");
        }
        try {
            SysRouteResponse response = logisticsService.searchRoute(type, orderId, false);
            if (response.isSuccess()) {
                return BaseDTO.buildSuccess(response.getRoutes());
            } else {
                log.error("用户获取[{}]的订单ID为[{}]的路由信息出错，错误信息为：{}:{}", type, orderId,
                        response.getErrorCode(), response.getE());
                return BaseDTO.buildError("300", "快递查询出错");
            }
        } catch (Throwable e) {
            log.error("用户获取[{}]的订单ID为[{}]的路由信息出错", type, orderId, e);
            return BaseDTO.buildError("系统异常");
        }
    }
}
